package com.example.weather_study.Activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatSpinner;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.weather_study.Adapter.FutureWeatherAdapter;
import com.example.weather_study.R;
import com.example.weather_study.bean.DayWeatherBean;
import com.example.weather_study.bean.WeatherBean;
import com.example.weather_study.util.NetUtil;
import com.example.weather_study.util.OKHttpUtil;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {
    // 1、定义
    private AppCompatSpinner mSpinner;

    // 2、适配器
    private ArrayAdapter<String> mSpAdater;


    //3 、数据资源
    private String[] mCities;
    // 添加组件进来
    private TextView tvWeather,tvTem,tvTemLowHigh,tvWin,tvAir;
    private ImageView ivWeather;
    private RecyclerView rlvFutureWeather;
    private FutureWeatherAdapter mWeatherAdapter;

    // 创建Handler
    private Handler mHandler = new Handler(Looper.myLooper()){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if(msg.what==0){
                String weather = (String) msg.obj;
                Log.e("fan","---主线程收到天气数据----weather----"+weather);

                // json解析
                Gson gson = new Gson();
                WeatherBean weatherBean = gson.fromJson(weather,WeatherBean.class);
                Log.e("fan","--解析后的数据----weather----"+weatherBean.toString());

                updateUIOfWeather(weatherBean);

            }
        }
    };

    private void updateUIOfWeather(WeatherBean weatherBean) {
        // 获取数据第一步，应该是判断是否为空
        if(weatherBean==null)   return;
        //获取 天 数据
        List<DayWeatherBean> dayWeathers = weatherBean.getDayWeatherBeanList();
        DayWeatherBean todayWeather = dayWeathers.get(0);
        if(todayWeather==null)  return;
        //把数据展示
        tvTem.setText(todayWeather.getTem());
        tvWeather.setText(todayWeather.getWea()+"("+todayWeather.getDate()+todayWeather.getWeek()+")");
        tvTem.setText(todayWeather.getTem());
        tvWeather.setText(todayWeather.getWea()+"("+todayWeather.getDate()+todayWeather.getWeek()+")");
        tvTemLowHigh.setText(todayWeather.getTem2()+"~"+ todayWeather.getTem1());
        tvWin.setText(todayWeather.getWin()[0]+todayWeather.getWinSpeed());
        tvAir.setText("空气："+todayWeather.getAir()+ todayWeather.getAirLevel()+"\n"+todayWeather.getAirTips());
        //
        ivWeather.setImageResource(getImgResOfWeather(todayWeather.getWea_Img()));

        // 未来天气的展示
        // 先要删除当天的天气
        dayWeathers.remove(0);
        //设置数据适配器，布局方向
        mWeatherAdapter = new FutureWeatherAdapter(this,dayWeathers);
        rlvFutureWeather.setAdapter(mWeatherAdapter);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false);
        rlvFutureWeather.setLayoutManager(linearLayoutManager);
        
    }


    // 这个API 没有返回图片，只返回了一个 标志位，需要自己将本地图片和标志 一一对应
    private int getImgResOfWeather(String weaStr){
        int result = 0;
        switch (weaStr){
            case "qing":
                result = R.drawable.biz_plugin_weather_qing;
                break;
            case "yin":
                result = R.drawable.biz_plugin_weather_yin;
                break;
            case "yu":
                result = R.drawable.biz_plugin_weather_yu ;
                break;
            case"bingbao":
                result = R.drawable.biz_plugin_weather_bingbao;
                break;
            case "wu":
                result = R.drawable.biz_plugin_weather_wu;
                break;
            case "shachen":
                result =R.drawable.biz_plugin_weather_shachen;
                break;
            case "lei":
                result = R.drawable.biz_plugin_weather_lei;
                break;
            default:
                result=R.drawable.biz_plugin_weather_xue ;
                break;
        }
        return result;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView(); // 刚开始因为没有用这个方法，吃了好多苦

    }
    private void initView(){
        mSpinner = findViewById(R.id.sp_city);
        mCities = getResources().getStringArray(R.array.cities);// 提前写好的城市名
        mSpAdater = new ArrayAdapter<>(this,R.layout.sp_item_layout,mCities);
        mSpinner.setAdapter(mSpAdater);
        // 点击事件
        mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String selectedCity = mCities[position];
                getWeatherOfCity(selectedCity); // 调用方法
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

        tvWeather =findViewById(R.id.tv_weather);
        tvAir = findViewById(R.id.tv_air);
        tvTem = findViewById(R.id.tv_tem);
        tvTemLowHigh = findViewById(R.id.tv_tem_low_high);
        tvWin = findViewById(R.id.tv_win);
        ivWeather=findViewById(R.id.iv_weather);
        rlvFutureWeather = findViewById(R.id.rlv_future_weather);



    }

    private void getWeatherOfCity(String selectedCity) {
        // 开启子线程，请求网络
        new Thread(new Runnable() {
            @Override
            public void run() {
                // 请求网络
                String weatherOfCity = NetUtil.getWeatherofCity(selectedCity);

                //使用handler 将数据传递给主线程
                Message message = Message.obtain();
                message.what=0;
                message.obj = weatherOfCity;
                mHandler.sendMessage(message);


            }
        }).start();


    }
}

